package com.syh.jn.scm.domain.operation.entity.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.dto.operation.system.permission.PermissionMto;
import com.syh.jn.scm.dto.operation.system.permission.PermissionSto;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class Permission extends AbstractSimpleAggregate<Long> {
	public Permission() {
		this.del = Short.valueOf("1");
	}

	public Permission(Long id,Long typeId,String permissionName, String permissionUrl, String permissionMethod) {
		this.id=id;
		this.permissionName = permissionName;
		this.permissionUrl = permissionUrl;
		this.permissionMethod = permissionMethod;
		this.typeId = typeId;
		this.del = Short.valueOf("1");
	}

	public Permission(PermissionSto sto) {
		ExtAssert.notNull(sto, "对象为空");
		ValidationUtils.validate(sto);
		initDefault();
		this.del = Short.valueOf("1");
		ExtBeanUtils.copyProperties(sto,this,false);
	}

	public void modify(PermissionMto mto){
		ExtAssert.notNull(mto, "对象为空");
		ValidationUtils.validate(mto);
		this.del = Short.valueOf("1");
		ExtBeanUtils.copyProperties(mto,this,false);
		this.lastUpdateTimeStamp=System.currentTimeMillis();
	}
	public void destory(){
		//角色-权限关联表中有数据,就不能删除权限
		/*OperationRoleRepository roleRepository= (OperationRoleRepository) SpringContextUtils.getBean(OperationRoleRepository.class);
		List<RoleTmpl> roles=roleRepository.getRolesByPermissionId(this.id);
		if(roles !=null && roles.size() >0){
			throw  new BusinessException("当前权限有角色关联,不能删除");
		}*/
	}

	private String permissionName;
	private String permissionUrl;
	private String permissionMethod;
	private Long typeId;
	private String terminalType;
	private Short del;

	public String getDetail() {
		return permissionUrl+(permissionMethod !=null? "=="+permissionMethod:"");
	}
}
